﻿using CinemaMaestro.ViewModels;
using CinemaMaestro.WindowsApplication.EndPoints;
using MetroFramework;
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CinemaMaestro.WindowsApplication.Category
{
    public partial class CategoriesForm : MetroForm
    {
        private CategoryEndPoints endPoints;

        public CategoriesForm(HttpClient client)
        {
            this.endPoints = new CategoryEndPoints(client);
            InitializeComponent();
            PopulateCategoriesListView();

            deleteButton.Enabled = false;
            updateButton.Enabled = false;
        }    

        private async void PopulateCategoriesListView()
        {
            categoriesListView.Items.Clear();
            try
            {
                List<CategoryViewModel> languages = await endPoints.GetAllCategoriesAsync();
                foreach (var language in languages)
                {
                    var item = new ListViewItem();
                    item.Text = language.Id.ToString();
                    item.SubItems.Add(language.Name);
                    categoriesListView.Items.Add(item);
                }
            }
            catch
            {
                MetroMessageBox.Show(this, "Cannot retrieve categories!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }         
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            nameTextBox.Text = String.Empty;
            categoriesListView.SelectedIndices.Clear();
            deleteButton.Enabled = false;
            updateButton.Enabled = false;
            createButton.Enabled = true;
        }

        private async void createButton_Click(object sender, EventArgs e)
        {
            if (nameTextBox.Text != String.Empty)
            {
                try
                {
                    var category = new CategoryViewModel { Name = nameTextBox.Text };
                    await endPoints.CreateCategoryAsync(category);
                }
                catch
                {
                    MetroMessageBox.Show(this, "Cannot create category!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                finally
                {
                    PopulateCategoriesListView();
                    nameTextBox.Text = String.Empty;
                }         
            }
            else
            {
                MetroMessageBox.Show(this, "Please provide a name for the category!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void categoriesListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (categoriesListView.SelectedItems.Count == 1)
            {
                deleteButton.Enabled = true;
                updateButton.Enabled = true;
                createButton.Enabled = false;
                nameTextBox.Text = categoriesListView.SelectedItems[0].SubItems[1].Text;
            }
        }

        private async void updateButton_Click(object sender, EventArgs e)
        {
            
            deleteButton.Enabled = false;
            updateButton.Enabled = false;   
            if (nameTextBox.Text != String.Empty)
            {
                try
                {
                    int selectedId = Int32.Parse(categoriesListView.SelectedItems[0].SubItems[0].Text);
                    var category = new CategoryViewModel
                    {
                        Id = selectedId,
                        Name = nameTextBox.Text
                    };
                    await endPoints.UpdateCategoryAsync(category);
                }
                catch
                {
                    MetroMessageBox.Show(this, "Cannot update category!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                finally
                {
                    PopulateCategoriesListView();
                    categoriesListView.SelectedIndices.Clear();
                    nameTextBox.Text = String.Empty;
                    createButton.Enabled = true;
                }   
            }
            else
            {
                MetroMessageBox.Show(this, "Please provide a name for the category to be updated!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void CategoriesForm_MouseDown(object sender, MouseEventArgs e)
        {
            categoriesListView.SelectedIndices.Clear();
            deleteButton.Enabled = false;
            updateButton.Enabled = false;
            createButton.Enabled = true;
            nameTextBox.Text = String.Empty;
        }

        private async void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                deleteButton.Enabled = false;
                updateButton.Enabled = false;
                int selectedId = Int32.Parse(categoriesListView.SelectedItems[0].SubItems[0].Text);
                await endPoints.DeleteCategoryAsync(selectedId);
            }
            catch
            {
                MetroMessageBox.Show(this, "Cannot delete category!", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            finally
            {
                PopulateCategoriesListView();
                categoriesListView.SelectedIndices.Clear();
                nameTextBox.Text = String.Empty;
                createButton.Enabled = true;
            }           
        }
    }
}
